var Lazy = {
		eCatch:{},
		eHandle:0,
		addEvent:function(o,e,func) {
			if(o.addEventListener) {
				o.addEventListener(e,func,false);
			}else{
				o.attachEvent("on"+e,func);
			}
			this.eCatch[++this.eHandle] = {"handler":func};
			return this.eHandle;
		},
		removeEvent:function(o,e,func) {
			if(o.addEventListener) {
				o.removeEventListener(e,this.eCatch[func].handler,false);
			}else{
				o.detachEvent("on"+e,this.eCatch[func].handler);
			}
		},
		create:function(o) {
			o.loading = false;
			o.timmer = undefined;
			o.time_act = 0;
			var imgList;
			o.imgList = [];
			if(document.querySelectorAll) {
				imgList = document.querySelectorAll('#' + o.lazyId + ' img');
			}else{
				imgList = document.getElementById(o.lazyId).getElementsByTagName("img");
			}
			for(var i=0,l=imgList.length; i<l; i++) {
				if(imgList[i].getAttribute(o.trueSrc)) {
					o.imgList.push(imgList[i]);
				}
			}
			o.imgCount = o.imgList.length;
			return o;
      },

		checkPhone:function(ua) {
			if (ua.indexOf("android") > -1 || ua.indexOf("iphone") > -1 || ua.indexOf("ipod") > -1 || ua.indexOf("ipad") > -1) {
				this.isPhone = true;
			}else{
				this.isPhone = false;
			}
		},

		checkLazyLoad:function(ua) {
			if(ua.indexOf("opera mini") > -1) {
				return false;
			}else{
				return true;
			}
		},
		init:function(o) {
			if (o.imgCount < 1) return;
			var ua = navigator.userAgent.toLowerCase();
			if(this.checkLazyLoad(ua)) {
				this.checkPhone(ua);
				o.e1 = this.addEvent(window,"scroll",this.load(o) );
				o.e2 = this.addEvent(window,"touchmove",this.load(o) );
				o.e3 = this.addEvent(window,"touchend",this.load(o) );
				this.loadTime(o);
			}else{
				this.loadOnce(o);
			}
		},
		getImgTop:function(o) {
			var imgTop = 0;
			while(o.offsetParent) {
				imgTop += o.offsetTop;
				o = o.offsetParent
			}
			return imgTop;
		},
		load:function(o) {
			return function() {
				if(o.loading == true) return;
				o.loading = true;
				if(o.time_act && ((1*new Date()-o.time_act)>o.delay_tot)) {
					o.timmer && clearTimeout(o.timmer);
					Lazy.loadTime(o);
				}else{
					o.timmer && clearTimeout(o.timmer);
					o.timmer = setTimeout(function() {
						Lazy.loadTime(o);
					},o.delay);
				}
				o.loading = false;
			}
		},
		setSrc:function(o,l) {
			o.setAttribute("src",o.getAttribute(l));
			o.removeAttribute(l);
		},
		loadTime:function(o) {
			o.time_act = 1*new Date();
			var winH,winTop,winTot;
			if (this.isPhone) {
				winH = window.screen.height;
				winTop = window.scrollY;
				winTot = winTop + winH;
			}else{
				winH = document.documentElement.clientHeight || document.body.clientHeight;
				winTop = Math.max(document.documentElement.scrollTop, document.body.scrollTop);
				winTot = winH + winTop;
			}
			if(!o.offset) {o.offset = winH;}
			var imgCache = [];
			for(var i = 0; i < o.imgCount; i++) {
				var img = o.imgList[i],imgTop;
				if(img.getBoundingClientRect) {
					imgTop = img.getBoundingClientRect().top + winTop;
				}else{
					imgTop = this.getImgTop(img);
				}
				if(imgTop > (winTop - o.offset) && imgTop < (winTot + o.offset) ) {
					if(imgTop > winTop && imgTop < winTot) {
						this.setSrc(img,o.trueSrc);
					}else{
						imgCache.push(img);
					}
					o.imgList.splice(i,1);
					i--;
					o.imgCount--;
				}
			}
			var imgCacheL = imgCache.length;
			if(imgCacheL) {
				for(var i = 0; i < imgCacheL; i++) {
					var img = imgCache[i]
					this.setSrc(img,o.trueSrc);
				}
			}
			if(o.imgCount == 0) {
				this.removeEvent(window,"scroll",o.e1);
				this.removeEvent(window,"touchmove",o.e2);
				this.removeEvent(window,"touchend",o.e3);
			}
		},
		loadOnce:function(o) {
			for(var i = 0; i < o.imgCount; i++) {
				var img = o.imgList[i];
				img.setAttribute("src",img.getAttribute(o.trueSrc));
			}
		}
	};

	(function(){
		 if(autoloadimg){
			  var autoloadimg  = $.cookie('autoloadimg');
			  var imgList = document.getElementsByName("LazyloadImg");
			  for(var i=0, len = imgList.length; i<len; i++){
					var img = imgList[i];
					var src2 = img.getAttribute("src2");
					if(src2){
						 img.src = src2;
					}
			  }
		 }else{
			  	var xx = Lazy.create({
					lazyId:"post_list",
					trueSrc:'src2',
					offset:2000,
					delay:100,
					delay_tot:1000
				});
				Lazy.init(xx);
		 }
	})();